#ifndef NP_BUILDER_H
#define NP_BUILDER_H

#include <config/Structures.h>
#include <utils/XFile.h>
#include <utils/XString.h>
#include <boost/lexical_cast.hpp>
#include <string.h>
#include <config/Config.h>

class Builder
{
public:
  Builder(const configInfo::ConfigInfo& config);

  ~Builder(void);

  void loadConfig();

  void loadTheme();

  bool createChapter(const configInfo::ConfigInfo& config, articleInfo::ArticleInfo& article, const chapterInfo::ChapterInfo& chapter);

  void createIndex(articleInfo::ArticleInfo& article, const chapterInfo::ChapterInfo* chapters, const int len);

private:
  mover::TemplateInfo m_template;

  configInfo::ConfigInfo m_config;

  std::string m_content;

  std::string m_txtContent;

  XFile m_file;

  int m_indexCols;

  void setDirValue(const std::string& key, std::string& dir);

  void setUrlValue(const std::string& key, std::string& url);

  //void replaceString(const int flag, string& dest);
  inline void replaceString( const int flag, std::string& dest)
  {
    switch(flag)
    {
    case 1://create index
      {
        XString::replaceAll(dest,"&", "&amp;");
        XString::replaceAll(dest,"<", "&gt;");
        XString::replaceAll(dest,">", "&lt;");
        XString::replaceAll(dest,"\n", "");
        XString::replaceAll(dest,"\r", "");
      }
      break;
    case 2:
      {
        XString::replaceAll(dest,"\r", "");
        XString::replaceAll(dest,"\n\n", "\n");
        XString::replaceAll(dest,"\n", "<br /><br />");
      }
    }
  }

  inline void initializeReplaceTemplete(std::string& input)
  {
    XString::replaceAll(input, "{?$jieqi_top_bar?}", m_template.JieqiDefine["JIEQI_TOP_BAR"]);
    XString::replaceAll(input, "{?$jieqi_bottom_bar?}", m_template.JieqiDefine["JIEQI_BOTTOM_BAR"]);
    XString::replaceAll(input, "{?$jieqi_banner?}", m_template.JieqiDefine["JIEQI_BANNER"]);
    XString::replaceAll(input, "{?$meta_keywords?}", m_template.JieqiDefine["JIEQI_META_KEYWORDS"]);
    XString::replaceAll(input, "{?$meta_description?}", m_template.JieqiDefine["JIEQI_META_DESCRIPTION"]);
    XString::replaceAll(input, "{?$meta_copyright?}", m_template.JieqiDefine["JIEQI_META_COPYRIGHT"]);
    XString::replaceAll(input, "{?$copy_info?}", m_template.JieqiDefine["JIEQI_META_COPYRIGHT"]);
    XString::replaceAll(input, "{?$jieqi_sitename?}", m_template.JieqiDefine["JIEQI_SITE_NAME"]);
    XString::replaceAll(input, "{?$jieqi_url?}", m_template.JieqiDefine["JIEQI_URL"]);
    XString::replaceAll(input, "{?$jieqi_main_url?}", m_template.JieqiDefine["JIEQI_URL"]);
    if(m_template.JieqiArticleConfigs["staticurl"].length() == 0)
    {
      XString::replaceAll(input, "{?$new_url?}", m_template.JieqiDefine["JIEQI_URL"]);
    }
    else
    {
      XString::replaceAll(input, "{?$new_url?}", m_template.JieqiDefine["staticurl"]);
    }
    if(m_template.JieqiArticleConfigs["dynamicurl"].length() == 0)
    {
      XString::replaceAll(input, "{?$dynamic_url?}", m_template.JieqiDefine["JIEQI_URL"]);
    }
    else
    {
      XString::replaceAll(input, "{?$dynamic_url?}", m_template.JieqiArticleConfigs["dynamicurl"]);
    }
    XString::replaceAll(input, "{?$jieqi_local_url?}", m_template.JieqiDefine["JIEQI_URL"]);
    XString::replaceAll(input, "{?$url_read?}", "");
    XString::replaceAll(input, "{?$jieqi_modules['article']['url']?}", m_template.JieqiDefine["JIEQI_URL"]);
  }

  inline void replaceTemplete(std::string& input,  articleInfo::ArticleInfo& article)
  {
    static char strTemp[1024] = {0};
    XString::replaceAll(input, "{?$author?}", article.author);
    XString::replaceAll(input, "{?$article_title?}", article.articlename);
    XString::replaceAll(input, "{?$book_title?}", article.articlename);
    if(article.intro.size() != 0)
    {
      XString::replaceAll(input, "{?$intro?}", article.intro);
    }
    sprintf(strTemp, "%s %s", article.lastvolume.c_str(), article.lastchapter.c_str());
    XString::replaceAll(input, "{?$jieqi_title?}", strTemp);
    XString::replaceAll(input, "{?$jieqi_volume?}", article.lastvolume);
    XString::replaceAll(input, "{?$jieqi_chapter?}", article.lastchapter);
    if(article.sortid == "0")
    {
      article.sortid = "1";
    }
    XString::replaceAll(input, "{?$sortname?}", m_template.JieqiSort[boost::lexical_cast<int>(article.sortid)]);
    XString::replaceAll(input, "{?$sortid?}", article.sortid);
    XString::replaceAll(input, "{?$jieqi_charset?}", m_config.charset);
    XString::replaceAll(input, "{?$meta_author?}", "x1");

    sprintf(strTemp, "/%d%s%s", boost::lexical_cast<int>(article.articleid)/mover::SIZE_FILE_SPLIT_UNIT, article.articleid.c_str(), m_template.JieqiArticleConfigs["htmlfile"].c_str());
    XString::replaceAll(input, "{?$url_fullpage?}", strTemp);

    sprintf(strTemp, "/%d%s.zip", boost::lexical_cast<int>(article.articleid)/mover::SIZE_FILE_SPLIT_UNIT, article.articleid.c_str());
    XString::replaceAll(input, "{?$url_download?}", strTemp);
    XString::replaceAll(input, "{?$url_fullpage?}", article.articleid + m_template.JieqiArticleConfigs["htmlfile"]);
    XString::replaceAll(input, "{?$articleid?}", article.articleid);

    sprintf(strTemp, "%d", boost::lexical_cast<int>(article.articleid)/ mover::SIZE_FILE_SPLIT_UNIT);
    XString::replaceAll(input, "{?$article_id|subdirectory?}", strTemp);
    XString::replaceAll(input, "{?$articleid|subdirectory?}", strTemp);
    XString::replaceAll(input, "{?$preview_page?}",  "index" + m_template.JieqiArticleConfigs["htmlfile"]);
    XString::replaceAll(input, "{?$next_page?}",  "index" + m_template.JieqiArticleConfigs["htmlfile"]);
    XString::replaceAll(input, "{?$index_page?}",  "index" + m_template.JieqiArticleConfigs["htmlfile"]);
    XString::replaceAll(input, "{?$article_id?}",  article.articleid);
    XString::replaceAll(input, "{?$chapter_id?}",  article.lastchapterid);
    XString::replaceAll(input, "{?$articleid?}",  article.articleid);

    sprintf(strTemp, "/%d", boost::lexical_cast<int>(article.articleid)/mover::SIZE_FILE_SPLIT_UNIT);
    XString::replaceAll(input, "{?$articlesubdir?}", strTemp);
    XString::replaceAll(input, "{?$chapterid?}",  article.lastchapterid);
    XString::replaceAll(input, "{?$url_bookroom?}",  m_template.JieqiDefine["JIEQI_URL"] + "/modules/article/");
    if(m_template.JieqiArticleConfigs["fakeinfo"].length() == 0)
    {
      XString::replaceAll(input, "{?$url_articleinfo?}",  m_template.JieqiDefine["JIEQI_URL"] + "/modules/article/articleinfo.php?id="+ article.articleid);
    }
    //else if(m_baseconfig.CmsVersion == "1.6")
    //{
    //  string tmp(JieqiArticleConfigs["fakeinfo"]);
    //  XString::replaceAll(tmp, "<{$id}>", article.articleid);

    //  sprintf(strTemp, "/%d", boost::lexical_cast<int>(article.articleid)/SIZE_FILE_SPLIT_UNIT);
    //  XString::replaceAll(tmp, "<{$id|subdirectory}>", strTemp);

    //  XString::replaceAll(input, "{?$url_articleinfo?}",  JieqiDefine["JIEQI_URL"] + tmp);
    //}
    //else if(JieqiArticleConfigs["fakeprefix"].length() == 0)
    if(m_template.JieqiArticleConfigs["fakeprefix"].length() == 0)
    {
      std::string temp;
      temp = m_template.JieqiDefine["JIEQI_URL"];
      temp += "/files/article/info/";
      temp += boost::lexical_cast<std::string>(boost::lexical_cast<int>(article.articleid) /mover::SIZE_FILE_SPLIT_UNIT);
      temp += "/";
      temp += article.articleid;
      temp += m_template.JieqiArticleConfigs["fakefile"];
      XString::replaceAll(input, "{?$url_articleinfo?}", temp);
    }
    else
    {
      std::string temp;
      temp += m_template.JieqiDefine["JIEQI_URL"];
      temp += "/";
      temp += m_template.JieqiArticleConfigs["fakeprefix"];
      temp += "info/";
      temp += boost::lexical_cast<std::string>(boost::lexical_cast<int>(article.articleid) / mover::SIZE_FILE_SPLIT_UNIT);
      temp += "/";
      temp += article.articleid;
      temp += m_template.JieqiArticleConfigs["fakefile"];
      XString::replaceAll(input, "{?$url_articleinfo?}", temp);
    }
    XString::replaceAll(input, "{novelname}", article.articlename);
    XString::replaceAll(input, "{NovelName}", article.articlename);
    XString::replaceAll(input, "{?$articlename|urlencode?}", "");
  }
  
  inline void copyChapterText(const configInfo::ConfigInfo& config, const chapterInfo::ChapterInfo& targetChapter, char* txtUrl)
  {
    static char sourcePath[256] = {0};
    static char targetPath[256] = {0};
    if (config.sourceflag == "copy")
    {
      sprintf(sourcePath, "%s/files/article/txt/%d/%s/%s.txt", config.sourcefilepath.c_str(),
        boost::lexical_cast<int>(targetChapter.sourcearticleid) / mover::SIZE_FILE_SPLIT_UNIT,
        targetChapter.sourcearticleid.c_str(), targetChapter.sourcechapterid.c_str());
      sprintf(targetPath, "%s/%d/%s/%s.txt", m_template.TxtDir.c_str(),
        boost::lexical_cast<int>(targetChapter.articleid) / mover::SIZE_FILE_SPLIT_UNIT,
        targetChapter.articleid.c_str(), targetChapter.chapterid.c_str());
      m_file.readFile(sourcePath, m_txtContent);
      if (m_file.writeFile("document.write(\"" + m_txtContent + "\");", targetPath))
      {
        sprintf(txtUrl, "<script src=\"%s/files/article/txt/%d/%s/%s.txt\"></script>", config.domain.c_str(), 
          boost::lexical_cast<int>(targetChapter.articleid) / mover::SIZE_FILE_SPLIT_UNIT,
          targetChapter.articleid.c_str(), targetChapter.chapterid.c_str());
      }
    }
    else if (config.sourceflag == "js")
    {
//      sprintf(txtUrl, "<script src=\"%s/files/article/txt/%d/%s/%s.txt\"></script>", config.targetdomain.c_str(),
//        m_template.TxtUrl.c_str(), boost::lexical_cast<int>(targetChapter.sourcearticleid)/mover::SIZE_FILE_SPLIT_UNIT,
//        targetChapter.sourcearticleid.c_str(), targetChapter.sourcechapterid.c_str());
    }
  }
};

#endif // NP_BUILDER_H
